package com.xiaoma.verbal.webservice;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.service.UserService;
import com.xiaoma.tpo.utils.TPOUtils;
import com.xiaoma.verbal.dto.QuClassicVo;
import com.xiaoma.verbal.dto.Top3ForecastVo;
import com.xiaoma.verbal.dto.TpoVo;
import com.xiaoma.verbal.service.ForecastService;
import com.xiaoma.verbal.service.QuClassicService;
import com.xiaoma.verbal.service.QuTpoService;
import com.xiaoma.verbal.service.QuestionService;
import com.xiaoma.verbal.utils.HandleResultUtils;

@Controller
@RequestMapping("/service/verbal")
public class VerbalQuestionWS {
	@Autowired
	private ForecastService forecastService;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuClassicService quClassicService;
	@Autowired
	private QuTpoService quTpoService;
	@Autowired
	private UserService userService;
	
	private static Logger LOG = LoggerFactory.getLogger(VerbalQuestionWS.class);
	//旧的
	@RequestMapping(value = "/tpos", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> tops(Boolean checkUpdate, Long lastUpdateTime,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();

		try {
			TpoVo vo = this.quTpoService.findTposBycheckUpdate(checkUpdate, lastUpdateTime);
			result.put("content", vo);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}

	@RequestMapping(value = "/classic", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> classic( Boolean checkUpdate, Long lastUpdateTime,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			QuClassicVo classic = quClassicService.findClassicByCheckUpdate(checkUpdate, lastUpdateTime);
			result.put("content", classic);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}
	@RequestMapping(value = "/top3Forecast", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> top3Forecast( Boolean checkUpdate, Long lastUpdateTime,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Top3ForecastVo top3 = forecastService.findTop3ForecastByCheckUpdate(checkUpdate, lastUpdateTime);
			result.put("content", top3);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}
	@RequestMapping(value = "/checkUpdate", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> checkUpdate() {
		Map<String, Object> result = new HashMap<String, Object>();
		return result;
	}
	/**
	 *  
	 *  新的接口
	 *  
	 */
	//新的
	@RequestMapping(value = "/tposNew", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> topsNew(Boolean checkUpdate, Long lastUpdateTime,String token,Integer clsClassId,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(clsClassId == null || clsClassId <= 0){
			result.put("status", -2);
			result.put("message", "clsClassId can't be null");
			return result;
		}
		User user = userService.findByToken(token);
		if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;
            
        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }

		try {
			TpoVo vo = this.quTpoService.findTposBycheckUpdateNew(checkUpdate, lastUpdateTime,user.getId(),clsClassId);
			result.put("content", vo);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}
	//新的---classic
	@RequestMapping(value = "/classicNew", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> classicNew( Boolean checkUpdate, Long lastUpdateTime,Integer clsClassId,String token,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(clsClassId == null || clsClassId <= 0){
			result.put("status", -2);
			result.put("message", "clsClassId can't be null");
			return result;
		}
		User user = userService.findByToken(token);
		if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;
            
        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }
		try {
			QuClassicVo classic = quClassicService.findClassicByCheckUpdate(checkUpdate, lastUpdateTime,clsClassId,user.getId());
			result.put("content", classic);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}

		return result;
	}
	//top3Forecast-----新
	@RequestMapping(value = "/top3ForecastNew", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> top3ForecastNew( Boolean checkUpdate, Long lastUpdateTime,Integer clsClassId,String token,String platform) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(clsClassId == null || clsClassId <= 0){
			result.put("status", -2);
			result.put("message", "clsClassId can't be null");
			return result;
		}
		User user = userService.findByToken(token);
		if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;
            
        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }
		try {
			Top3ForecastVo top3 = forecastService.findTop3ForecastByCheckUpdateNew(checkUpdate, lastUpdateTime, clsClassId, user.getId());
			result.put("content", top3);
			if("1".equals(platform)){
				result.put("status", 1);
			}else{
				HandleResultUtils.success(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			if("1".equals(platform)){
				result.put("status", 0);
				result.put("message", "系统错误，请稍后再试！");
			}else{
				HandleResultUtils.failure(result);
				
			}
		}
		
		return result;
	}

}
